#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>

struct ListNode {
    int val;
    struct ListNode* next;
    ListNode(int x) : val(x), next(NULL) {}
};

class PalindromeList {
public:

    ListNode* MidNode(ListNode* phead)
    {
        ListNode* fast, * slow;
        fast = slow = phead;
        while (fast && fast->next)
        {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }

    ListNode* Reverse(ListNode* phead)
    {
        ListNode* n1, * n2, * n3;
        if (phead == NULL)
            return NULL;
        n1 = NULL;
        n2 = phead;
        n3 = phead->next;
        while (n2)
        {
            n2->next = n1;
            n1 = n2;
            n2 = n3;
            if (n3)
                n3 = n3->next;
        }
        return n1;
    }

    bool chkPalindrome(ListNode* head)
    {
        // write code here
        ListNode* tmp = head;
        ListNode* mid = MidNode(tmp);
        ListNode* cur = Reverse(mid);
        while (head && cur)
        {
            if (head->val != cur->val)
            {
                return false;
            }
            head = head->next;
            cur = cur->next;
        }
        return true;
    }
};